package com.liao.liaowenlong;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.Collections;

public class FragmentMain1LayoutFragment extends Fragment implements View.OnClickListener, ItemListViewLayoutAdapter.CallBack {

    private ListView listView;
    private Context context;
    private ArrayList<BookInfo> books;
    private ItemListViewLayoutAdapter adapter;
    private Intent intent;
    private MyReceiver myReceiver;

    private BookInfo deleteBookInfo = null;
    private BookInfo insertBookInfo = null;

    @Override
    public void delete(BookInfo bookInfo) {
        this.deleteBookInfo = bookInfo;
        intent.putExtra("type", "delete");
        intent.putExtra("bookName", bookInfo.getName());
        context.startService(intent);
    }

    public class MyReceiver extends BroadcastReceiver {
        public static final String ACTION = "com.liao.liaowenlong";

        @Override
        public void onReceive(Context context, Intent intent) {
            ArrayList<BookInfo> bookInfos = (ArrayList<BookInfo>) intent.getSerializableExtra("books");
            String type = intent.getStringExtra("type");
            if ("insert".equals(type)) {
                long number = intent.getLongExtra("number", -1);
                if (number > 0) {
                    books.add(0, insertBookInfo);
                    Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
                }
            } else if ("query".equals(type)) {
                books.addAll(bookInfos);
            } else if ("delete".equals(type)) {
                long number = intent.getLongExtra("number", -1);
                if (number > 0) {
                    books.remove(deleteBookInfo);
                    Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show();
                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main1_layout, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listView = (ListView) view.findViewById(R.id.listView);
        view.findViewById(R.id.btn_insert).setOnClickListener(this);
        context = getContext();
        books = new ArrayList<>();

        adapter = new ItemListViewLayoutAdapter(context, books, this);
        listView.setAdapter(adapter);

        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MyReceiver.ACTION);
        context.registerReceiver(myReceiver, filter);
        initService();
    }

    private void initService() {
        intent = new Intent(context, MyService.class);
        intent.putExtra("type", "query");
        context.startService(intent);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_insert:
                alertDialog();
                break;
        }
    }

    private void alertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        @SuppressLint("InflateParams") View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
        builder.setView(inflate);
        final EditText et_name = inflate.findViewById(R.id.et_name);
        final EditText et_author = inflate.findViewById(R.id.et_author);
        final EditText et_sum = inflate.findViewById(R.id.et_sum);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String author = et_author.getText().toString();
                String name = et_name.getText().toString();
                String sum = et_sum.getText().toString();
                int i;
                try {
                    i = Integer.parseInt(sum);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    return;
                }
                BookInfo bookInfo = new BookInfo();
                bookInfo.setSum(i);
                bookInfo.setName(name);
                bookInfo.setAuthor(author);
                FragmentMain1LayoutFragment.this.insertBookInfo = bookInfo;
                intent.putExtra("type", "insert");
                intent.putExtra("bookInfo", bookInfo);
                context.startService(intent);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context.stopService(intent);
        context.unregisterReceiver(myReceiver);
    }
}
